package com.zzl.study.auth.domain.converter;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzl.study.auth.domain.RegisteredClient;
import com.zzl.study.auth.domain.TokenSettings;
import com.zzl.study.auth.domain.dto.RegisteredClientDto;
import com.zzl.study.auth.domain.dto.TokenSettingsDto;
import org.springframework.core.convert.converter.Converter;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Date;

/**
 * @author: zhangzl
 * @date: 2023/12/8 9:58
 * @version: 1.0
 * @description: 注册客户端DTO转换器
 */
@Component
public class RegisteredClientDtoConverter implements Converter<RegisteredClient, RegisteredClientDto> {

    @Override
    public RegisteredClientDto convert(@NonNull RegisteredClient source) {
        RegisteredClientDto registeredClientDto = new RegisteredClientDto();
        registeredClientDto.setId(source.getId());
        registeredClientDto.setClientId(source.getClientId());
        registeredClientDto.setClientIdIssuedAt(source.getClientIdIssuedAt() == null ? null : new Date(source.getClientIdIssuedAt().getTime()));
        registeredClientDto.setClientSecret(source.getClientSecret());
        registeredClientDto.setClientSecretExpiresAt(source.getClientSecretExpiresAt() == null ? null : new Date(source.getClientSecretExpiresAt().getTime()));
        registeredClientDto.setClientName(source.getClientName());
        registeredClientDto.setClientAuthenticationMethods(StringUtils.hasText(source.getClientAuthenticationMethods()) ? Arrays.stream(source.getClientAuthenticationMethods().split(",")).toList() : null);
        registeredClientDto.setAuthorizationGrantTypes(StringUtils.hasText(source.getAuthorizationGrantTypes()) ? Arrays.stream(source.getAuthorizationGrantTypes().split(",")).toList() : null);
        registeredClientDto.setRedirectUris(StringUtils.hasText(source.getRedirectUris()) ? Arrays.stream(source.getRedirectUris().split(",")).toList() : null );
        registeredClientDto.setPostLogoutRedirectUris(StringUtils.hasText(source.getPostLogoutRedirectUris()) ? Arrays.stream(source.getPostLogoutRedirectUris().split(",")).toList() : null );
        registeredClientDto.setScopes(StringUtils.hasText(source.getScopes()) ? Arrays.stream(source.getScopes().split(",")).toList() : null );
        if (StringUtils.hasText(source.getTokenSettings())) {
            JSONObject tokenSettingsJson = JSONUtil.parseObj(source.getTokenSettings());
            TokenSettingsDto tokenSettings = new TokenSettingsDto();
            Integer authorizationCodeTimeToLive = (Integer) tokenSettingsJson.getJSONArray(TokenSettings.AUTHORIZATION_CODE_TIME_TO_LIVE).get(1);
            tokenSettings.setAuthorizationCodeTimeToLive(authorizationCodeTimeToLive.longValue());
            Integer accessTokenTimeToLive = (Integer) tokenSettingsJson.getJSONArray(TokenSettings.ACCESS_TOKEN_TIME_TO_LIVE).get(1);
            tokenSettings.setAccessTokenTimeToLive(accessTokenTimeToLive.longValue());
            Integer refreshTokenTimeToLive = (Integer) tokenSettingsJson.getJSONArray(TokenSettings.REFRESH_TOKEN_TIME_TO_LIVE).get(1);
            tokenSettings.setRefreshTokenTimeToLive(refreshTokenTimeToLive.longValue());
            registeredClientDto.setTokenSettings(tokenSettings);
        }
        return registeredClientDto;
    }

}
